package com.zhongyouyun.admin.service.imp;

import cn.hutool.core.lang.Dict;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhongyouyun.admin.model.DictModel;
import com.zhongyouyun.admin.model.MenuModel;
import com.zhongyouyun.admin.mapper.MenuMapper;
import com.zhongyouyun.admin.model.UserModel;
import com.zhongyouyun.admin.service.DictService;
import com.zhongyouyun.admin.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: shenyi
 * @Date: 2021-09-23
 * @Description: 资源表服务实现类
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class MenuServiceImpl extends ServiceImpl<MenuMapper, MenuModel> implements MenuService {

    @Autowired
    private DictService dictService;

    /*
     * @Author: shenyi
     * @Date: 2021-09-08
     * @Description: 分页查询
     * @param page 分页数据  lambdaQueryWrapper where 条件 和排序
     */
    @Override
    public IPage<MenuModel> selectPage(Page<MenuModel> page, QueryWrapper<MenuModel> queryWrapper) {
        return this.baseMapper.selectPage(page, queryWrapper);
    }


    /*
     * @Author: shenyi
     * @Date: 2021-09-08
     * @Description: 查询全部
     * @param   lambdaQueryWrapper where 条件 和排序
     */
    @Override
    public List<MenuModel> selectList(QueryWrapper<MenuModel> queryWrapper) {
        return this.baseMapper.selectList(queryWrapper);
    }

    /*
     * @Author: shenyi
     * @Date: 2021-09-08
     * @Description: 查询全部
     * @param   lambdaQueryWrapper where 条件 和排序
     */
    @Override
    public List<Tree<String>> findMenuTreeList(MenuModel menuModel) {
        List<MenuModel> menuModelList = this.baseMapper.findMenuTreeList(menuModel);
        List<DictModel> dictList = dictService.list();
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        List<Tree<String>> list = TreeUtil.build(menuModelList, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId().toString());
                    tree.setParentId(treeNode.getPId().toString());
                    tree.putExtra("name", treeNode.getName());
                    tree.putExtra("url", treeNode.getUrl());
                    tree.putExtra("pid", treeNode.getPId());
                    tree.putExtra("state", treeNode.getState());
                    tree.putExtra("perms", treeNode.getPerms());
                    tree.putExtra("icon", treeNode.getIcon());
                    tree.putExtra("type", treeNode.getType());
                    tree.putExtra("idDel", treeNode.getIdDel());
                    String perm = treeNode.getPerms();
                    if (StrUtil.isNotEmpty(perm)) {
                        String[] permStr = perm.split(";");
                        for (String s : permStr) {
                            dictList.forEach(dictModel -> {
                                String value = dictModel.getValue();
                                if (s.equals(value)) {
                                    List<MenuModel> modelList = (List<MenuModel>) tree.get("children");
                                    if (null == modelList) {
                                        modelList = new ArrayList<MenuModel>();
                                    }
                                    MenuModel model = new MenuModel();
                                    model.setName(dictModel.getDescription());
                                    model.setType("3");
                                    model.setId(dictModel.getId());
                                    model.setPId(treeNode.getId());
                                    model.setPerms(dictModel.getValue());
                                    modelList.add(model);
                                    tree.put("children", modelList);

                                }
                            });
                        }
                    }
                });
        return list;
    }

    @Override
    public List<Tree<Long>> findMenuTreeLists(MenuModel menuModel) {
        List<MenuModel> menuModelList = this.baseMapper.findMenuTreeList(menuModel);
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        List<Tree<Long>> list = TreeUtil.build(menuModelList, 0L, treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId());
                    tree.setParentId(treeNode.getPId());
                    tree.putExtra("name", treeNode.getName());
                    tree.putExtra("label", treeNode.getName());
                    tree.putExtra("url", treeNode.getUrl());
                    tree.putExtra("pid", treeNode.getPId());
                    tree.putExtra("state", treeNode.getState());
                    tree.putExtra("perms", treeNode.getPerms());
                    tree.putExtra("icon", treeNode.getIcon());
                    tree.putExtra("type", Integer.valueOf(treeNode.getType()));
                    tree.putExtra("idDel", treeNode.getIdDel());
                    tree.putExtra("parentName","顶级菜单");
                    menuModelList.forEach(model->{
                        if(model.getId().equals(treeNode.getPId()))
                        tree.put("parentName",model.getName());
                    });
                });
        return list;
    }
}
